/*
 * Copyright 2009 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.dailydev.wave.robot.servlet;

import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;

import org.dailydev.wave.robot.IRobot;
import org.dailydev.wave.robot.RobotException;

import com.google.wave.api.AbstractRobotServlet;
import com.google.wave.api.RobotMessageBundle;

/**
 * Implementation of {@link AbstractRobotServlet} integrating DailyDev Wave
 * Robot Engine.
 * 
 * @author Roman Bosak, DailyDev.org
 */
public class RobotServlet extends AbstractRobotServlet {

    /**
     * 
     */
    private static final long serialVersionUID = 1212366769906136983L;

    /**
     * {@link RobotServlet}'s logger
     */
    private static Logger log = Logger.getLogger(RobotServlet.class.getName());

    /**
     * 
     */
    private IRobot robot;

    /** {@inheritDoc} */
    @Override
    public final void processEvents(RobotMessageBundle bundle) {
        try {
            robot.processEvents(bundle);
        } catch (RobotException e) {
            log.log(Level.SEVERE, "Exception thrown during events processing.", e);
        }
    }

    /** {@inheritDoc} */
    @Override
    public void init(ServletConfig config) throws ServletException {
        super.init(config);

        String robotFactoryClassName = getFactoryClasName(config);
        try {
            Class<IRobotFactory> robotFactoryClass = (Class<IRobotFactory>) Class.forName(robotFactoryClassName);
            IRobotFactory robotFactory = robotFactoryClass.newInstance();
            robot = robotFactory.buildRobot(config);
        } catch (ClassNotFoundException e) {
            throw new ServletException("Unable to find robot factory class '" + robotFactoryClassName + "'", e);
        } catch (InstantiationException e) {
            throw new ServletException("Unable to instantiate robot factory class '" + robotFactoryClassName + "'", e);
        } catch (IllegalAccessException e) {
            throw new ServletException("Unable to instantiate robot factory class '" + robotFactoryClassName + "'", e);
        }
    }

    /**
     * @param config
     * @return
     */
    protected String getFactoryClasName(ServletConfig config) {
        return config.getServletContext().getInitParameter(IRobotFactory.ROBOT_FACTORY__PARAM_NAME);
    }
}
